angular.module("hyt.workbenchCtrl",[])

  .controller('workbenchCtrl', function(Storage,FeeInfo,$scope,$ionicPopover,$timeout) {
    var loginMsg=Storage.get('user');
    var feeInfo={
      flag:'1',
      userMsg:{
        username:loginMsg.username,
        password:loginMsg.password,
        uuid:loginMsg.uuid
      }
    };
    $scope.bill='周账单';
    $scope.feeItem='营业收入';
    FeeInfo.getFeeInfo(JSON.stringify(feeInfo));
    $scope.$on('FeeInfo.Updated', function() {
      var result=FeeInfo.getResult();
      if (result.rows!=undefined){
        $scope.income=result.rows.income;
        $scope.unincome=result.rows.unincome;
        $scope.trust_cost=result.rows.trust_cost;
        $scope.way_size=result.rows.way_size;
        $timeout(function () {
          var demo = new CountUp("myTargetElement", 0, $scope.income, 2, 1);
          demo.start();
        },500);
      }else {
        var demo = new CountUp("myTargetElement", 0, 0, 2, 1);
        demo.start();
      }
    });
    $scope.chooseTime=function($event){
      $scope.chooseTimeSlot.show($event);
    };
    $scope.chooseBill=function(i){
      $scope.chooseTimeSlot.hide();
      if (i==1){
        feeInfo.flag='0';
        $scope.bill='日账单';
        $scope.feeItem='营业收入';
        FeeInfo.getFeeInfo(JSON.stringify(feeInfo));
      }else if (i==2){
        feeInfo.flag='1';
        $scope.bill='周账单';
        $scope.feeItem='营业收入';
        FeeInfo.getFeeInfo(JSON.stringify(feeInfo));
      }else if (i==3){
        feeInfo.flag='2';
        $scope.bill='月账单';
        $scope.feeItem='营业收入';
        FeeInfo.getFeeInfo(JSON.stringify(feeInfo));
      }
    };
    $scope.chooseFeeInfo=function($event){
      $scope.chooseFee.show($event);
    };
    $scope.chooseItem=function(i){
      $scope.chooseFee.hide();
      if (i==1){
        var demo = new CountUp("myTargetElement", 0, $scope.income, 2, 2);
        demo.start();
        $scope.feeItem='营业收入';
      }else if (i==2){
        var demo = new CountUp("myTargetElement", 0, $scope.unincome, 2, 2);
        demo.start();
        $scope.feeItem='费用支出';
      }else if (i==3){
        var demo = new CountUp("myTargetElement", 0, $scope.trust_cost, 2, 2);
        demo.start();
        $scope.feeItem='代收货款';
      }else if (i==4){
        var demo = new CountUp("myTargetElement", 0, $scope.way_size, 2, 2);
        demo.start();
        $scope.feeItem='运单数量';
      }
    };
    $ionicPopover.fromTemplateUrl('templates/tab-workbench/chooseFee.html', {
      scope: $scope
    }).then(function(popover) {
      $scope.chooseFee = popover;
    });
    $ionicPopover.fromTemplateUrl('templates/tab-workbench/chooseTimeSlot.html', {
      scope: $scope
    }).then(function(popover) {
      $scope.chooseTimeSlot = popover;
    });
    var lat=Storage.get('lat');
    var long=Storage.get('long');
    var point = new BMap.Point(long,lat);
    var geoc = new BMap.Geocoder();
    geoc.getLocation(point, function(rs){
      var addComp = rs.addressComponents;
      Storage.set('gpsProvince',addComp.province);
      Storage.set('gpsCity',addComp.city);
      Storage.set('gpsArea',addComp.district);
    });
  })
  .controller('workbenchSearchCtrl',function($scope,Storage,MyWaybills,$cordovaBarcodeScanner,$state){
    $scope.showloading=false;
    var loginMsg=Storage.get('user');
    $scope.search={
      parameter:{
        WAYBILL_NO:''
      },
      userMsg:{
        username:loginMsg.username,
        password:loginMsg.password,
        uuid:loginMsg.uuid
      }
    };
    $scope.$on('$ionicView.enter', function() {
      if ($scope.search.parameter.WAYBILL_NO==''){
        $("#searchWb").focus();
      }
    });
    document.onkeydown=function(event){
      if(event.keyCode==13&&$scope.search.parameter.WAYBILL_NO!=''){
        $scope.showloading=true;
        MyWaybills.getWaybillData($scope.search);
      }else if (event.keyCode==13&&$scope.search.parameter.WAYBILL_NO==''){
        $scope.waybillData='';
      }
    };
    $scope.qrScanner=function(){
      document.addEventListener("deviceready", function () {
        $state.go('workbenchSearch');
        $cordovaBarcodeScanner
          .scan()
          .then(function(barcodeData) {
            $scope.search.parameter.WAYBILL_NO=barcodeData.text;
            if ($scope.search.parameter.WAYBILL_NO!=''){
              MyWaybills.getWaybillData($scope.search);
            }
          }, function(error) {
            // An error occurred
          });
      }, false);
    };
    $scope.$on('WaybillList.Updated', function() {
      $scope.waybillData=MyWaybills.getResult();
      $scope.showloading=false;
    });
    //下拉刷新
    $scope.doRefresh=function(){
      if ($scope.search.parameter.WAYBILL_NO!=''){
        MyWaybills.getWaybillData($scope.search);
      }
      $scope.$broadcast('scroll.refreshComplete');
    };
    $scope.goDetail=function(id,STATUS){
      Storage.set('wayId',id);
      $state.go('detailTab.detail',{STATUS:STATUS});
    };
  })
  .controller('noticeListCtrl',function($scope,Storage,NoticeList,$stateParams){
    $scope.showloading=true;
    var loginMsg=Storage.get('user');
    var noticeSearch={
      userMsg:{
        username:loginMsg.username,
        password:loginMsg.password,
        uuid:loginMsg.uuid
      }
    };
    NoticeList.getNoticeData(noticeSearch);
    $scope.$on('Notice.Updated', function() {
      $scope.noticeData=NoticeList.getResult();
      $scope.showloading=false;
    });
    //下拉刷新
    $scope.doRefresh=function(){
      NoticeList.getNoticeData(noticeSearch);
      $scope.$broadcast('scroll.refreshComplete');
    };
    //上拉加载更多
    $scope.loadMore=function(){
      NoticeList.getMoreNoticeData(noticeSearch);
      $scope.$broadcast('scroll.infiniteScrollComplete');
    };
    //下一页
    $scope.hasNextPage = function() {
      return NoticeList.hasNextPage();
    };
    if ($stateParams.title!=undefined||$stateParams.content!=undefined){
      $scope.notice={
        title:$stateParams.title,
        content:$stateParams.content
      }
    }
  })
